#!/bin/bash

BLACK="\033[1;30m"
RED='\033[1;31m'    # 红
GREEN='\033[1;32m'  # 绿
YELLOW='\033[1;33m' # 黄
BLUE='\033[1;34m'   # 蓝
PINK='\033[1;35m'   # 紫
CYAN='\033[1;36m'   # 青
WHITE='\033[1;37m'   # 白
CLS='\033[0m'       # 清除颜色

# 变量定义
STD_PROJECT_ROOT_NAME=StdPeriph_Lib_Template
STD_LIB_NAME=STM32F10x_StdPeriph_Lib_V3.6.0
STD_STARTUP_NAME=startup_stm32f10x_hd.s
HAL_PROJECT_ROOT_NAME=STM32Cube_FW_F1_Template
HAL_LIB_NAME=STM32Cube_FW_F1_V1.8.0
PROJECT_TYPE="xxx lib project"

STD_PROJECT_DRIVER_DIRNAME="STM32F10x_FWLib"
HAL_PROJECT_DRIVER_DIRNAME="HAL_lib"
PROJECT_DIR_NAME[0]=""                # STM32F10x_FWLib/HAL_lib 存放 ST 官方提供的库函数源码文件
PROJECT_DIR_NAME[1]="CORE"            # CORE 用来存放核心文件和启动文件
PROJECT_DIR_NAME[2]="USER"            # USER 用来放我们的代码工程文件
PROJECT_DIR_NAME[3]="OBJ"             # OBJ  用来存放编译过程文件以及 hex 文件               
PROJECT_DIR_NAME[4]="HARDWARE"        # HARDWARE 存放用户编写的外设驱动文件
declare -A PROJECT_DIR_PATH           # 定义一个字典，后边组合生成键值对，字典必须先声明

CURRENT_PATH=$(pwd)
PROJECT_ROOT_PATH=${CURRENT_PATH}
LIB_PATH=${CURRENT_PATH}

# 提示信息打印函数
function echo_menu()
{
    echo -e "${GREEN}================================================${CLS}"
    echo -e ""
    echo -e "${WHITE}# CURRENT_PATH          : ${CURRENT_PATH}${CLS}"
    echo -e "${WHITE}# STD_PROJECT_ROOT_NAME : ${STD_PROJECT_ROOT_NAME}${CLS}"
    echo -e "${WHITE}# HAL_PROJECT_ROOT_NAME : ${HAL_PROJECT_ROOT_NAME}${CLS}"
    echo -e "${WHITE}# STD_LIB_NAME          : ${STD_LIB_NAME}${CLS}"
    echo -e "${WHITE}# HAL_LIB_NAME          : ${HAL_LIB_NAME}${CLS}"
    echo -e ""
    echo -e "${GREEN}* [0] 拷贝STM32工程所需标准库文件(标准库工程)"
    echo -e "${GREEN}* [1] 拷贝STM32工程所需HAL库文件(HAL库工程)"
    echo -e "${GREEN}================================================${CLS}"

    read -p "选择功能,默认选择0:" choose
    if [ "${choose}" == "0" ] || [ "${choose}" == "" ];then # [0] 拷贝STM32工程所需标准库文件
        PROJECT_ROOT_PATH=${CURRENT_PATH}/${STD_PROJECT_ROOT_NAME}
        LIB_PATH=${CURRENT_PATH}/${STD_LIB_NAME}
        PROJECT_DIR_NAME[0]=${STD_PROJECT_DRIVER_DIRNAME}
        PROJECT_TYPE="STD_lib_project"
    elif [ "${choose}" == "1" ];then                        # [0] 拷贝STM32工程所需HAL库文件
        PROJECT_ROOT_PATH=${CURRENT_PATH}/${HAL_PROJECT_ROOT_NAME}
        LIB_PATH=${CURRENT_PATH}/${HAL_LIB_NAME}
        PROJECT_DIR_NAME[0]=${HAL_PROJECT_DRIVER_DIRNAME}
        PROJECT_TYPE="HAL_lib_project"
    fi

    echo -e "${GREEN}================================================${CLS}"
    echo -e "${GREEN}相关动态参数如下：${CLS}"
    echo -e "${PINK}# PROJECT_TYPE      : ${PROJECT_TYPE}${CLS}"
    echo -e "${PINK}# PROJECT_ROOT_PATH : ${PROJECT_ROOT_PATH}${CLS}"
    echo -e "${PINK}# LIB_PATH          : ${LIB_PATH}${CLS}"
    echo -e "${PINK}# PROJECT_DIR_NAME  : ${PROJECT_DIR_NAME[*]}${CLS}"
    echo -e "${GREEN}================================================${CLS}"
}

function copy_hal_lib_files()
{
    cp -pr ${LIB_PATH}/Drivers/STM32F1xx_HAL_Driver/Inc ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[0]}]}
    cp -pr ${LIB_PATH}/Drivers/STM32F1xx_HAL_Driver/Src ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[0]}]}

    cp -p ${LIB_PATH}/Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/arm/startup_stm32f103xe.s ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[1]}]}
    cp -p ${LIB_PATH}/Drivers/CMSIS/Include/cmsis_armcc.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[1]}]}
    cp -p ${LIB_PATH}/Drivers/CMSIS/Include/cmsis_armclang.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[1]}]}
    cp -p ${LIB_PATH}/Drivers/CMSIS/Include/cmsis_compiler.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[1]}]}
    cp -p ${LIB_PATH}/Drivers/CMSIS/Include/cmsis_version.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[1]}]}
    cp -p ${LIB_PATH}/Drivers/CMSIS/Include/core_cm3.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[1]}]}

    cp -p ${LIB_PATH}/Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f1xx.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]}
    cp -p ${LIB_PATH}/Drivers/CMSIS/Device/ST/STM32F1xx/Include/system_stm32f1xx.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]}
    cp -p ${LIB_PATH}/Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f103xe.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]}

    cp -p ${LIB_PATH}/Projects/STM3210E_EVAL/Templates/Inc/main.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]}
    cp -p ${LIB_PATH}/Projects/STM3210E_EVAL/Templates/Inc/stm32f1xx_hal_conf.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]}
    cp -p ${LIB_PATH}/Projects/STM3210E_EVAL/Templates/Inc/stm32f1xx_it.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]}

    cp -p ${LIB_PATH}/Projects/STM3210E_EVAL/Templates/Src/main.c ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]}
    cp -p ${LIB_PATH}/Projects/STM3210E_EVAL/Templates/Src/system_stm32f1xx.c ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]}
    cp -p ${LIB_PATH}/Projects/STM3210E_EVAL/Templates/Src/stm32f1xx_it.c ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]}
    cp -p ${LIB_PATH}/Projects/STM3210E_EVAL/Templates/Src/stm32f1xx_hal_msp.c ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]}
}

function copy_std_lib_files()
{
    cp -pr ${LIB_PATH}/Libraries/STM32F10x_StdPeriph_Driver/src ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[0]}]} # STM32F10x_FWLib
    cp -pr ${LIB_PATH}/Libraries/STM32F10x_StdPeriph_Driver/inc ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[0]}]} # STM32F10x_FWLib

    cp -p ${LIB_PATH}/Libraries/CMSIS/CM3/CoreSupport/core_cm3.c ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[1]}]} # CORE
    cp -p ${LIB_PATH}/Libraries/CMSIS/CM3/CoreSupport/core_cm3.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[1]}]} # CORE
    cp -p ${LIB_PATH}/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/arm/${STD_STARTUP_NAME} ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[1]}]} # CORE

    cp -p ${LIB_PATH}/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/stm32f10x.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]} # USER
    cp -p ${LIB_PATH}/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.c ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]} # USER
    cp -p ${LIB_PATH}/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]} # USER

    cp -p ${LIB_PATH}/Project/STM32F10x_StdPeriph_Template/main.c ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]} # USER
    cp -p ${LIB_PATH}/Project/STM32F10x_StdPeriph_Template/stm32f10x_conf.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]} # USER
    cp -p ${LIB_PATH}/Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]} # USER
    cp -p ${LIB_PATH}/Project/STM32F10x_StdPeriph_Template/stm32f10x_it.h ${PROJECT_DIR_PATH[${PROJECT_DIR_NAME[2]}]} # USER
    
}

function copy_files_to_project()
{
    if [ ! -d "${PROJECT_ROOT_PATH}" ];then
        mkdir -v ${PROJECT_ROOT_PATH}
    fi

    cd ${PROJECT_ROOT_PATH}
    echo -e "${WHITE}# CURRENT_PATH      : $(pwd)${CLS}"

    # 创建相关目录
    for DIR_NAME in ${PROJECT_DIR_NAME[@]}
        do
            if [ ! -d "${PROJECT_ROOT_PATH}/${DIR_NAME}" ];then
                mkdir -v ${PROJECT_ROOT_PATH}/${DIR_NAME}
            else
                echo -e "${YELLOW}[WARN]${DIR_NAME} 目录已存在!!!${CLS}"
            fi
            PROJECT_DIR_PATH[${DIR_NAME}]=${PROJECT_ROOT_PATH}/${DIR_NAME}
        done
    #打印所有key值
    echo -e "${GREEN}[INFO]${!PROJECT_DIR_PATH[*]}${CLS}"
    #打印所有value
    echo -e "${GREEN}[INFO]${PROJECT_DIR_PATH[*]}${CLS}"
    # 拷贝相关文件
    if [ "${PROJECT_TYPE}" == "STD_lib_project" ];then
        copy_std_lib_files
        exit
    elif [ "${PROJECT_TYPE}" == "HAL_lib_project" ];then
        copy_hal_lib_files
        exit
    fi
    
}

# 功能实现
echo_menu
copy_files_to_project
